package com.xuexisheqv.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.xuexisheqv.auth.application.convert.AuthRolePermissionDTOConverter;
import com.xuexisheqv.auth.application.dto.AuthRolePremissionDTO;
import com.xuexisheqv.auth.entity.Result;
import com.xuexisheqv.auth.domain.entity.AuthRolePremissionBO;
import com.xuexisheqv.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限 controller
 */
@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePremissionDTO authRolePremissionDTO ) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePremissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePremissionDTO.getPermissionIdList()),
                    "权限关联不能为空");
            Preconditions.checkNotNull(authRolePremissionDTO.getRoleId(),
                    "角色id不能为空");
            AuthRolePremissionBO authRolePremissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDtoToBo(authRolePremissionDTO);
            return Result.ok(authRolePermissionDomainService.add(authRolePremissionBO));
        } catch (Exception e) {
            log.error(" RolePermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }


}
